import axios from './intercept'
import { useRouter } from 'vue-router'
const router = useRouter()

//基本请求方法
const Get = ({ url = '', params = {}, headers = {} }) => {
    return axios({
        method: 'get',
        url,
        params,
        headers
    })
}

const Post = ({ url = '', params = {}, headers = {} }) => {
    return axios({
        method: 'post',
        url,
        data: params,
        headers
    })
}

const Delete = ({ url = '', params = {}, headers = {} }) => {
    return axios({
        method: 'delete',
        url,
        params,
        headers
    })
}

const Put = ({ url = '', params = {}, headers = {} }) => {
    return axios({
        method: 'put',
        url,
        data: params,
        headers
    })
}

/*
 * 所有请求
 */


//post
const Login = (params = {}) => {
    return new Promise((resolve, reject) => {
        Post({
            url: '/user/login',
            params
        })
            .then((res: any) => {
                if (res.data.token) {
                    localStorage.setItem('app_token', res.data.token)
                }
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}

//get
const privacy = () => {
    return new Promise((resolve, reject) => {
        Get({
            url: '/api/agreement/privacyAgreement'
        })
            .then((res: any) => {
                resolve(res)
            })
            .catch((err: any) => {
                reject(err)
            })
    })
}


export default {
    Get,
    Post,
    Delete,
    Put,
    Login,
    privacy,
}
